package org.aksw.xoperator.xoperatorwebconfig.client;

import org.aksw.xoperator.conf.AgentConfiguration;
import org.aksw.xoperator.xmpp.JabberAccount;
import org.aksw.xoperator.xmpp.JabberClientConfiguration;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class AgentConfigGenerator extends Composite implements ClickListener {
	
	private final AgentConfigServiceAsync ags;
	
	private VerticalPanel vpanel = new VerticalPanel();
	
	private JabberAccountVerificationWidget mainwidget;
	
	private JabberAccountVerificationWidget proxywidget;
	
	private RemoteRdfVerificationWidget rdfwidget;
	
	private SparlEndpointVerificationWidget sparqlwidget;
	
	private AgentConfiguration conf;
	
	

	
	
	

	
	public AgentConfigGenerator() {
	      // Initialize the service.
	      //
	      ags = (AgentConfigServiceAsync) GWT.create(AgentConfigService.class);

	      // By default, we assume we'll make RPCs to a servlet, but see
	      // updateRowData(). There is special support for canned RPC responses.
	      // (Which is a totally demo hack, by the way :-)
	      // 
	      ServiceDefTarget target = (ServiceDefTarget) ags;

	      // Use a module-relative URLs to ensure that this client code can find
	      // its way home, even when the URL changes (as might happen when you
	      // deploy this as a webapp under an external servlet container).
	      String moduleRelativeURL = GWT.getModuleBaseURL() + "xOperatorWebConfig";
	      target.setServiceEntryPoint(moduleRelativeURL);
		
		
		conf = new AgentConfiguration();
		constructInterface();
	}
	
	
	private void constructInterface(){
		mainwidget = new JabberAccountVerificationWidget();
		vpanel.add(mainwidget);
		
		this.initWidget(vpanel);
		
		
		
	}
	
	
	
	@Override
	protected void onLoad() {
		// TODO Auto-generated method stub
		super.onLoad();
	}


	public void onClick(Widget sender) {
		// TODO Auto-generated method stub
		
	}
	
	public class JabberAccountVerificationWidget extends Composite implements ClickListener{
		
		private VerticalPanel rows = new VerticalPanel();
		
		private TextBox userName = new TextBox();
		private PasswordTextBox password = new PasswordTextBox();
		private TextBox server = new TextBox();
		private TextBox port = new TextBox();
		private TextBox resource = new TextBox();
		private Button verify = new Button("verify!");
		private Label checkResult = new Label("Press verify");
		
		private boolean verified = false;
		
		public JabberAccountVerificationWidget() {
			verify.addClickListener(this);
			this.initWidget(rows);
			HorizontalSplitPanel usern = new HorizontalSplitPanel();
			usern.add(new Label("Full Username (JID) "));
			usern.add(userName);
			rows.add(usern);
			rows.add(checkResult);
			
		}
		/**
		 *
		 */
		public void onClick(Widget sender) {
			verify.setEnabled(false);
			checkResult.setText("Verifying");
			ags.verifyConnection(this.getData(), new AsyncCallback<Boolean>(){

				public void onFailure(Throwable caught) {
					checkResult.setText("Error: " + caught.getLocalizedMessage());
					
				}

				public void onSuccess(Boolean result) {
					verified = result.booleanValue();
					if(result.booleanValue()){
						checkResult.setText("Working");
					}else{
						checkResult.setText("Faulty");
					}
					
				}
				
			});
			
		}
		
		public JabberAccount getData(){
			JabberAccount acc = new JabberAccount();
			acc.setJid(userName.getText());
			acc.setPassword(password.getText());
			acc.setServer(server.getText());
			acc.setPort(Integer.decode(port.getText()));
			acc.setResource(resource.getText());
			return acc;
			
		}
		
	}
	
	public class SparlEndpointVerificationWidget{
		
	}
	
	public class RemoteRdfVerificationWidget{
		
	}
	
	

}
